<div id="fi_comptes_form">
	
	<!--// Nouveau compte -->
	<div id="fi_newcpt_form">
		<form name="ajouter" method="post" action="index.php?a=fi&sa=addcpt&d=fi&p=cpt">
			<span class="list-titre"><?php abbr("fi_op_cpt_new");?> | </span>
				<?php abbr("fi_op_cpt_nom");?> : <input type="text" name="nom" size="20" />&nbsp;&nbsp;
				<?php abbr("fi_op_cpt_devise");?> : <input type="text" name="devise" size="10" maxlength="10" />&nbsp;&nbsp;
				<input type="submit" value="<?php say("fi_cpt_creer"); ?>" style="font-size:0.8em;" />
		</form>
	</div>
	
	<!-- //Liste des comptes -->
	<div id="fi_cpt_list">
		<?php
		$connexion = new Connexion();
		$sql = "SELECT * FROM fi_comptes;";
		$req = $connexion->query($sql);
		while($data = $connexion->fetch_assoc($req))
		{
			// Données du compte
			$id = $data['id'];
			$nom = $data['nom'];
			if(isset($data['devise'])) $devise = $data['devise']; else $devise = config("devise");
			
			// Connexion
			$conn = new Connexion();
			
			// Combien d'opérations sur ce compte ?
			$sql = "SELECT * FROM fi_operations WHERE compte='$id';";
			$count = $conn->count($conn->query($sql));
			if($count == 0)		$nombre = donner("fi_op_op_none");
			elseif($count == 1) $nombre = $count." ".donner("fi_op_op");
			elseif($count > 1) 	$nombre = $count." ".donner("fi_op_op_s");
			else 				$nombre = donner("fi_op_op_none");
			
			// Quel est le solde de ce compte ?
			$sql = "SELECT SUM(montant) AS solde FROM fi_operations WHERE compte='$id';";
			$req = $conn->fetch_assoc($conn->query($sql));
			$solde = round($req['solde'],2);
			
			// Equilibrage du solde
			$equilibre = $data['equilibre'];
			$solde = $solde + $equilibre;
			
			// Combien de crédit, débit, total en mouvement sur le compte ?
			$sql = "SELECT SUM(montant) as csolde FROM fi_operations WHERE compte='$id' and montant > 0;";
			$req = $conn->fetch_assoc($conn->query($sql));
			$csolde = round($req['csolde'],2);
			$sql = "SELECT SUM(montant) as dsolde FROM fi_operations WHERE compte='$id' and montant < 0;";
			$req = $conn->fetch_assoc($conn->query($sql));
			$dsolde = round($req['dsolde'],2);
			$tsolde = $csolde - $dsolde; // dsolde est négatif, moins par moins égale plus
			
			// Formatage
			$solde = str_replace('.',config('sepdec'),$solde);
			$equilibre = str_replace('.',config('sepdec'),$equilibre);
			$csolde = str_replace('.',config('sepdec'),$csolde);
			$dsolde = str_replace('.',config('sepdec'),$dsolde);
			$tsolde = str_replace('.',config('sepdec'),$tsolde);
			
			// Les 5 dernières opérations enregistrées
			$sql = "SELECT * FROM fi_operations WHERE compte='$id' ORDER BY id DESC LIMIT 0,5;";
			$last5 = "";
			$rq = $conn->query($sql);
			while($req = $conn->fetch_assoc($rq))
			{
				// Couleur rouge ou vert selon que le montant est négatif ou positif
				if($req['montant'] < 0) $req['montant'] = "<span style=\"color:#F00;\">".$req["montant"]."</span>";
				else $req['montant'] = "<span style=\"color:#008040;\">".$req["montant"]."</span>";
				
				$last5 .= "[".$req["montant"]."] [".$req['date_enregistrement']."] ".$req["nom"]."<br />";
			}
			
			// Modifier/supprimer
			$modifier = "<a href=\"#\" onclick=\"javascript:fi_cpt_mod($id)\">".donner("fi_cpt_modifier")."</a>";
			$supprimer = "<a href=\"#\" onclick=\"javascript:fi_cpt_sup($id)\">".donner("fi_cpt_supprimer")."</a>";

			// Affichage
			?>
			<div class="fi_cpt_item">
				
				<div class="fi_cpt_item_title">
					<?php echo $nom; ?>
				</div>
				
				<div class="fi_cpt_item_mod">
					<?php
					echo "($modifier|$supprimer)<br />"
						."<div id=\"fi_cpt_mod_$id\" style=\"display:none;\">"
							."<form name=\"modifier\" method=\"post\" action =\"index.php?a=fi&sa=modcpt&d=fi&p=cpt\">"
							."<input type=\"text\" name=\"nom\" value=\"$nom\" />"
							."<input type=\"hidden\" name=\"id\" value=\"$id\" />"
							."<input type=\"submit\" value=\"".donner("ok")."\">"
							."</form>"
						."</div>"
						."<div id=\"fi_cpt_sup_$id\" style=\"display:none;\">"
							."<form name=\"supprimer\" method=\"post\" action =\"index.php?a=fi&sa=supcpt&d=fi&p=cpt\">"
							."<input type=\"hidden\" name=\"id\" value=\"$id\" />"
							."<input type=\"submit\" value=\"".donner("fi_cpt_supprimer2")."\">"
							."</form>"				
						."</div>";
					?>
				</div>
				
				<!-- GRAPHIQUES -->
				<div class="fi_cpt_item_graph">
					<img src="graphs/fi_solde_evolution.php?cid=<?php echo $id; ?>" />
					<br />
					<img src="graphs/fi_op_nombre.php?cid=<?php echo $id; ?>" />
					<br />
					<img src="graphs/fi_op_poids.php?cid=<?php echo $id; ?>" />
				</div>
				
				<div class="fi_cpt_item_details">
					<?php say("fi_cpt_operations"); ?> : <b><?php echo $nombre; ?></b>
					<br />
					<?php say("fi_cpt_solde"); ?> : <b><?php echo "$solde $devise"; ?></b>
					<br />
					<?php say("fi_cpt_csolde"); ?> : <b><?php echo "$csolde $devise"; ?></b>
					<br />
					<?php say("fi_cpt_dsolde"); ?> : <b><?php echo "$dsolde $devise"; ?></b>
					<br />
					<?php say("fi_cpt_tsolde"); ?> : <b><?php echo "$tsolde $devise"; ?></b>
					<br /><br />
					<?php say("fi_cpt_oplist"); ?> <br />	<?php echo $last5; ?>
					<br /><br />
					<?php say("fi_cpt_equilibre"); ?> : 
					<form name="equilibre" method="post" action="index.php?a=fi&sa=equilibre&d=fi&p=cpt">
						<input type="hidden" value="<?php echo $id; ?>" name="id" />
						<input type="text" value="<?php echo $equilibre; ?>" name="equilibre" />
						&nbsp;<input type="submit" value="<?php say("fi_cpt_equilibre_submit"); ?>" />
					</form>
				</div>
				
			</div>
			<p>&nbsp;</p>
		<?php } ?>
		</div>
	</div>
	
</div>